﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using AjaxControlToolkit;
using Telerik.Web.UI;

/// <summary>
/// Summary description for SDSValidation
/// </summary>
public class Validation
{
	public Validation()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static Validation GetInstance() 
    {
        return new Validation();
    }
    public void ValidateFeild(Control[] c, string typeOfValidate)
    {

        if (typeOfValidate.ToLower() == "isnumber")
        {
            var textbox = from con in c
                          where con is TextBox
                          select con;
            List<Control> t = textbox.ToList<Control>();
            foreach (Control text in t)
            {
                ((TextBox)text).Attributes.Add("onKeyUp", "_isNumber(this)");
            }
        }
    }
    public bool IsStringDate(string date) 
    {
        try { DateTime.ParseExact(date,"dd/MM/yyyy",null); return true; }
        catch { }
        return false;
    }
    public static void ValidateControlMoney(ref Panel pnl, string controlToValidate)
    {
        ValidatorCalloutExtender ve = new ValidatorCalloutExtender();
        RegularExpressionValidator re = new RegularExpressionValidator();
        re.ID = "re_01" + controlToValidate + new Random().Next(1, 999);
        ve.ID = "ve_01" + controlToValidate + new Random().Next(1, 999);
        ve.TargetControlID = re.ID;
        re.ControlToValidate = controlToValidate;
        re.ErrorMessage = Constant.VALID_MONEY;
        re.Display = ValidatorDisplay.None;
        re.ValidationExpression = Constant.EXPRESSION_MONEY;
        pnl.Controls.Add(re);
        pnl.Controls.Add(ve);
    }

    public static void ValidateRequiredField(ref Panel pnl, Control[] controlToValidates, string validateGroup)
    {
        foreach (Control controlToValidate in controlToValidates)
        {
            if (controlToValidate.GetType() == typeof(TextBox) || controlToValidate.GetType() == typeof(RadTextBox) || controlToValidate.GetType() == typeof(RadDatePicker) || (controlToValidate.GetType() == typeof(DropDownList) || controlToValidate.GetType() == typeof(RadNumericTextBox)))
            {
                ValidateRequiredField(ref pnl, controlToValidate, validateGroup);
            }
            else if (controlToValidate.GetType() == typeof(DropDownList) || controlToValidate.GetType() == typeof(RadComboBox))
            {
                ValidateRequiredFieldDropDown(ref pnl, controlToValidate, validateGroup);
            }
        }
    }

    public static void ValidateRequiredField(ref Panel pnl, Control[] controlToValidates, string validateGroup,bool isPopup)
    {
        foreach (Control controlToValidate in controlToValidates)
        {
            if (controlToValidate.GetType() == typeof(TextBox) || controlToValidate.GetType() == typeof(RadTextBox) || controlToValidate.GetType() == typeof(RadDatePicker) || (controlToValidate.GetType() == typeof(DropDownList) || controlToValidate.GetType() == typeof(RadNumericTextBox)))
            {
                ValidateRequiredField(ref pnl, controlToValidate, validateGroup, isPopup);
            }
            else if (controlToValidate.GetType() == typeof(DropDownList) || controlToValidate.GetType() == typeof(RadComboBox))
            {
                ValidateRequiredFieldDropDown(ref pnl, controlToValidate, validateGroup, isPopup);
            }
        }
    }

    public static void ValidateRequiredField(ref Panel pnl, Control controlToValidate, string validateGroup)
    {
        ValidatorCalloutExtender ve = new ValidatorCalloutExtender();
        RequiredFieldValidator re = new RequiredFieldValidator();
        re.ID = "re_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.ID = "ve_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.TargetControlID = re.ID;
        re.ControlToValidate = controlToValidate.ID;
        re.ErrorMessage = Constant.REQ_FEILD_VALIDATE;
        re.Display = ValidatorDisplay.None;
        re.ValidationGroup = validateGroup;
        pnl.Controls.Add(re);
        pnl.Controls.Add(ve);
    }

    public static void ValidateRequiredField(ref Panel pnl, Control controlToValidate, string validateGroup,bool isPopup)
    {
        ValidatorCalloutExtender ve = new ValidatorCalloutExtender();
        RequiredFieldValidator re = new RequiredFieldValidator();
        re.ID = "re_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.ID = "ve_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.TargetControlID = re.ID;
        re.ControlToValidate = controlToValidate.ID;
        re.ErrorMessage = Constant.REQ_FEILD_VALIDATE;
        re.Display = ValidatorDisplay.None;
        re.ValidationGroup = validateGroup;
        pnl.Controls.Add(re);

        if (isPopup) pnl.Controls.Add(ve);
    }

    //public static void ValidateRequiredFieldDropDown(ref Panel pnl, Control[] controlToValidates)
    //{
    //    foreach (Control controlToValidate in controlToValidates) 
    //    {
    //        ValidateCompareField(ref pnl, controlToValidate, "-1", Constant.REQ_FEILD_VALIDATE);
    //    }
    //}

    public static void ValidateRequiredFieldDropDown(ref Panel pnl, Control controlToValidate, string validateGroup,bool isPopup)
    {
        ValidateCompareField(ref pnl, controlToValidate, Constant.REQ_FEILD, Constant.REQ_FEILD_VALIDATE, validateGroup, isPopup);
    }

    public static void ValidateRequiredFieldDropDown(ref Panel pnl, Control controlToValidate, string validateGroup)
    {
        ValidateCompareField(ref pnl, controlToValidate, Constant.REQ_FEILD, Constant.REQ_FEILD_VALIDATE,validateGroup);
    }

    public static void ValidateCompareField(ref Panel pnl, Control controlToValidate, string compareWith, string msgError, string validateGroup)
    {
        ValidatorCalloutExtender ve = new ValidatorCalloutExtender();
        CompareValidator re = new CompareValidator();
        re.ID = "re_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.ID = "ve_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.TargetControlID = re.ID;
        re.ControlToValidate = controlToValidate.ID;
        re.Operator = ValidationCompareOperator.NotEqual;
        re.ValueToCompare = compareWith;
        re.ErrorMessage = msgError;
        re.Display = ValidatorDisplay.None;
        re.ValidationGroup = validateGroup;
        pnl.Controls.Add(re);
        pnl.Controls.Add(ve);
    }

    public static void ValidateCompareField(ref Panel pnl, Control controlToValidate, string compareWith, string msgError, string validateGroup,bool isPopup)
    {
        ValidatorCalloutExtender ve = new ValidatorCalloutExtender();
        CompareValidator re = new CompareValidator();
        re.ID = "re_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.ID = "ve_01" + controlToValidate.ID + new Random().Next(1, 999);
        ve.TargetControlID = re.ID;
        re.ControlToValidate = controlToValidate.ID;
        re.Operator = ValidationCompareOperator.NotEqual;
        re.ValueToCompare = compareWith;
        re.ErrorMessage = msgError;
        re.Display = ValidatorDisplay.None;
        re.ValidationGroup = validateGroup;
        pnl.Controls.Add(re);

        if (isPopup) pnl.Controls.Add(ve);
    }

    public static void ValidateControlNumberOnly(ref Panel pnl, string controlToValidate)
    {
        ValidatorCalloutExtender vqe = new ValidatorCalloutExtender();
        RegularExpressionValidator rqe = new RegularExpressionValidator();
        rqe.ID = "rqe_02" + new Random().Next(1,999);
        vqe.ID = "vqe_01" + new Random().Next(1, 999);
        vqe.TargetControlID = rqe.ID;
        rqe.ControlToValidate = controlToValidate;
        rqe.ErrorMessage = Constant.VALID_NUMBER_ONLY;
        rqe.Display = ValidatorDisplay.None;
        rqe.ValidationExpression = Constant.EXPRESSION_NUMBER_ONLY;
        pnl.Controls.Add(rqe);
        pnl.Controls.Add(vqe);
    }
}
